﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="JobsAdminMediaDefinitionsViewModel.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the JobsAdminMediaDefinitionsViewModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DocProcessingWorkflow.Models.JobsAdmin
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using DocProcessingWorkflow.Models.Helper;
  using DocProcessingWorkflow.Models.MediaDefinition;
  using DocProcessingWorkflow.Models.MediaStock;
  using Entities;

  public class JobsAdminMediaDefinitionsViewModel
  {
    public readonly List<MediaDefinitionViewModel> MediaDefinitions = new List<MediaDefinitionViewModel>();

    public PagingInfoViewModel PagingInfo { get; set; }

    public void AddMediaDefinitions(PagedResult<MediaDefinitionEntity> mediaDefinitions)
    {
      foreach (MediaDefinitionEntity mediaDefinition in mediaDefinitions.Results)
      {
        var medDefModel = new MediaDefinitionViewModel
                                                 {
                                                   MediaDefId = mediaDefinition.MediaDefID,
                                                   QueueName = mediaDefinition.QueueName,
                                                   QueueNameSymbollic = mediaDefinition.QueueNameSymbollic,
                                                   Print = mediaDefinition.Print.HasValue && mediaDefinition.Print.Value,
                                                   PdfOnly = mediaDefinition.PDF_Only.HasValue && mediaDefinition.PDF_Only.Value,
                                                   MailSortSettingId = mediaDefinition.MailSortSettingId.HasValue ? mediaDefinition.MailSortSettingId.Value : 0,
                                                   MailSorted = mediaDefinition.MailSortSettingId.HasValue && mediaDefinition.MailSortSettingId.Value != 0
                                                 };

        this.MediaDefinitions.Add(medDefModel);
      }

      this.PagingInfo = new PagingInfoViewModel
      {
        CurrentPage = mediaDefinitions.CurrentPage,
        TotalItems = mediaDefinitions.TotalItems,
        ItemsPerPage = mediaDefinitions.ItemsPerPage,
        TotalPages = mediaDefinitions.TotalPages,
        StartRow = mediaDefinitions.StartRow,
        EndRow = mediaDefinitions.EndRow
      };
    }

    public void AddMediaStocksForDefinition(Int32 mediaDefinitionId, IEnumerable<MediaStockEntity> mediaStocks)
    {
      MediaDefinitionViewModel mediaDefinition = this.MediaDefinitions.Single(md => md.MediaDefId == mediaDefinitionId);

      foreach (MediaStockEntity mediaStock in mediaStocks)
      {
        if (mediaStock.StockType == "Insert")
        {
          mediaDefinition.Inserts.Add(this.ToMediaStockViewModel(mediaStock));
        }
        else if (mediaStock.StockType == "Outer")
        {
          mediaDefinition.Envelopes.Add(this.ToMediaStockViewModel(mediaStock));
        }
        else if (mediaStock.StockType == "LaserStock")
        {
          mediaDefinition.Trays.Add(this.ToMediaStockViewModel(mediaStock));
        }
      }
    }

    private MediaStockViewModel ToMediaStockViewModel(MediaStockEntity mediaStock)
    {
      return new MediaStockViewModel
               {
                 MediaStockId = mediaStock.MediaStockId,
                 Code = mediaStock.Code,
                 Description = mediaStock.Description,
                 StationNumber = mediaStock.StationNumber,
                 Weight = mediaStock.Weight,
                 SizeX = mediaStock.SizeX,
                 SizeY = mediaStock.SizeY,
                 Facing = mediaStock.Facing,
                 Colour = mediaStock.Colour
               };
    }
  }
}